package com.city.controller.colligate;

import com.alibaba.fastjson.JSONArray;
import com.city.config.BaiduMap;
import com.city.config.Status;
import com.city.domain.city.CityCopnotePerson;
import com.city.domain.city.CityEvent;
import com.city.domain.pub.PubFilepath;
import com.city.domain.report.CtCopnote;
import com.city.domain.report.CtDoBan;
import com.city.domain.report.CtReport;
import com.city.domain.report.CtTask;
import com.city.domain.report.format.CtCopnoteFormatSql;
import com.city.format.CtCopnoteFormat;
import com.city.format.CtReportFormat;
import com.city.format.CtReportFormatSql;
import com.city.helper.AuthHelper;
import com.city.helper.TimeStampHelper;
import com.city.repository.report.CtReportFormatSqlRepository;
import com.city.repository.report.format.CtCopnoteFormatSqlRepository;
import com.city.repository.report.format.CtUserFoematRepository;
import com.city.service.city.CityCopnotePersonService;
import com.city.service.city.CityEventService;
import com.city.service.pub.PubFilepathService;
import com.city.service.pub.PubUserService;
import com.city.service.report.CtTaskService;
import com.city.service.report.ReportService;
import com.city.helper.FormatHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 雷楚桥
 * @deprecation 综合受理系统
 * @email leichuqiao@126.com
 * @date 2018-07-06 17:32
 */
@Controller
@RequestMapping("/colligate")
public class ColligateController {

    @Autowired
    private ReportService reportS;
    @Autowired
    private CityEventService eventS;
    @Autowired
    private FormatHelper formatH;
    @Autowired
    private AuthHelper authH;
    @Autowired
    private CityCopnotePersonService cityCopnotePersonService;
    @Autowired
    PubFilepathService filepathService;
    @Autowired
    private PubUserService pubUserService;
    @Autowired
    private CtTaskService ctTaskService;
    @Autowired
    private CtCopnoteFormatSqlRepository ctCopnoteFormatSqlRepository;
    @Autowired
    private CtReportFormatSqlRepository ctReportFormatSqlRepository;
    @Autowired
    private CtUserFoematRepository ctUserFoematRepository;


    /**
     * @deprecation 系统
     * @author 雷楚桥
     * @date 2018-07-06 10:55
     */
    @RequestMapping("/index")
    public String index(Map<String, Object> map) {
        return "/colligate/index";
    }


    /**
     * @deprecation 基于GIS社情民意登记受理
     * @author 雷楚桥
     * @date 2018-07-06 21:41
     */
    @RequestMapping("/dispose")
    public String dispose(Map<String, Object> map, @Param("page") Integer page, HttpSession session) {


        //每页显示的条数
        long pageSize = 20;

        long total = ctReportFormatSqlRepository.countReport(-1, 3);
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        List<CtReportFormatSql> ctReportFormatSqls = ctReportFormatSqlRepository.findDiaryByPage(-1, 3, startRow, pageSize);
        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));
        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));

        map.put("currentPage", page);
        map.put("reportList", ctReportFormatSqls);


        map.put("titleName", "基于gis上报受理");
        return "/colligate/dispose";
    }

    /**
     * @param id Integer id类型
     * @return String
     * @deprecation 社情民意详情
     * @author 雷楚桥
     * @date 18-7-17 下午3:48
     */
    @RequestMapping("/detail")
    public String detail(@Param("id") Integer id,
                         Map<String, Object> map) {

        //详情
        CtReport ctReport = reportS.findReportById(id);
        String taskId = ctReport.getTaskId();
        //获取时间
        CtTask ctTask = ctTaskService.findByTaskId(id);

        if(ctReport.getIscaseback() == null){
            //把评论放进去
            if(ctTask == null){
                CtTask ctTask1 = new CtTask();
                ctTask1.setCasebackComment(null);
                map.put("taskTime", ctTask1);
            }else {
                ctTask.setCasebackComment(null);
            }
        }
        map.put("taskTime", ctTask);
        map.put("taskId", taskId);

        map.put("reportFormat", formatH.ctReportFormat(ctReport));

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "详情");
        return "/colligate/detail";
    }


    /**
     * @param id Integer 民情日记id
     * @deprecation 民情日记详情
     * @author 雷楚桥
     * @date 2018/7/28 18:13
     */
    @RequestMapping("/copnote_detail")
    public String copnote_detail(@Param("id") Integer id,
                                 Map<String, Object> map) {

        CtCopnote copnote = reportS.findCopnoteById(id);
//        CityCopnotePerson cityCopnotePerson = cityCopnotePersonService.findByCopnoteId(id);
        List<CityCopnotePerson> cityCopnotePeople = cityCopnotePersonService.findAllById(id);
        CtCopnoteFormat format = formatH.ctCopnoteFormat(copnote);
//        format.setHandleManName(cityCopnotePerson.getName());


        String taskId = copnote.getTaskId();
//        //拿到文件列表
//        List<PubFilepath> filepathList = filepathService.findListByTaskId(copnote.getTaskId());
//
//        System.out.println(JSONArray.toJSON(filepathList));
        map.put("copnotePerson",cityCopnotePeople);
        map.put("format", format);
        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("taskId", taskId);
        map.put("titleName", "详情");
        return "/colligate/copnote_detail";
    }

    /**
     * @param id Integer gis事件id
     * @deprecation gis事件登记详情
     * @author 雷楚桥
     * @date 2018/7/28 18:49
     */
    @RequestMapping("/gis_detail")
    public String gis_detail(@Param("id") Integer id,
                             Map<String, Object> map) {

        CityEvent cityEvent = eventS.findById(id);

        map.put("format", formatH.cityEventFormat(cityEvent));
        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "详情");
        return "/colligate/gis_detail";
    }


    /**
     * @deprecation 社情民意处置督办
     * @author 雷楚桥
     * @date 18-7-17 下午5:31
     */
    @RequestMapping("/do_ban")
    public String supervise(Map<String, Object> map,@Param("page") Integer page,HttpSession session) {

        //每页显示的条数
        long pageSize = 15;

        long total = ctReportFormatSqlRepository.countReport(-1, 4);
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }


        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        List<CtReportFormatSql> ctReportFormatSqls = ctReportFormatSqlRepository.findDiaryByPage(-1, 4, startRow, pageSize);
        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));
        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));
        map.put("currentPage", page);
        map.put("formatList", ctReportFormatSqls);
        map.put("titleName", "社情民意处置督办");
        return "/colligate/do_ban";
    }

    /**
     * @deprecation 社情民意处置流程
     * @author 雷楚桥
     * @date 18-7-18 上午11:23
     */
    @RequestMapping("/process")
    public String process(Map<String, Object> map) {
        map.put("titleName", "社情民意处置流程");
        return "/colligate/process";
    }

    /**
     * @deprecation 其他问题接入[居民热线、领导交办]
     * @author 雷楚桥
     * @date 18-7-19 上午10:49
     */
    @RequestMapping("/other")
    public String other(Map<String, Object> map) {
        map.put("titleName", "其他问题接入");
        return "/colligate/other";
    }

    /**
     * @deprecation 指示灯预警
     * @author 雷楚桥
     * @date 18-7-19 上午11:00
     */
    @RequestMapping("/light")
    public String light(Map<String, Object> map) {

        // 查询所有的指示灯情况 TODO 未根据用户id进行查询
        List<CtDoBan> doBanList = reportS.findAllDoBanByLightEqZero();
        Map<Integer, Integer> repIdMap = new HashMap<>();

        doBanList.forEach(ctDoBan -> {
            repIdMap.merge(ctDoBan.getReportId(), 1, (a, b) -> a + b);
        });

        List<CtReportFormat> formatList = new ArrayList<>();


        for (Integer i : repIdMap.keySet()) {
            CtReport rep = reportS.findReportById(i);
            if(rep != null){
                CtReportFormat f = formatH.ctReportFormatCount(rep, repIdMap.get(i));
                formatList.add(f);
            }


        }

        map.put("formatList", formatList);
        map.put("titleName", "指示灯预警");
        return "/colligate/light";
    }


    /**
     * @deprecation 核查结案
     * @author 雷楚桥
     * @date 18-7-19 下午12:04
     */
    @RequestMapping("/checks")
    public String checks(Map<String, Object> map,@Param("page") Integer page,HttpSession session) {

        // 查询所有的已反馈的案件

        //每页显示的条数
        long pageSize = 15;

        long total = ctReportFormatSqlRepository.countReport(3, 6);
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        List<CtReportFormatSql> ctReportFormatSqls = ctReportFormatSqlRepository.findDiaryByPage(3, 6, startRow, pageSize);
        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));

        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));


//        List<CtReportFormatSql> sqls = ctReportFormatSqlRepository.findDiary(3, 6);
//        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));
        map.put("currentPage", page);


        map.put("formatList", ctReportFormatSqls);
        map.put("titleName", "核查结案");
        return "/colligate/checks";
    }

    /**
     * @param id Integer id类型
     * @return String
     * @deprecation 核查结案详情
     * @author 雷楚桥
     * @date 18-8-17 下午3:48
     */
    @RequestMapping("/checks_detail")
    public String checks_detail(@Param("id") Integer id,
                                Map<String, Object> map) {

        //详情
        CtReport ctReport = reportS.findReportById(id);
        CtReportFormat reportFormat = formatH.ctReportFormat(ctReport);
        reportFormat.setCheckManName(ctUserFoematRepository.findByUserId(ctReport.getCheckManId()).getName());
        if (ctReport.getHechaId() != null) {
            reportFormat.setHechaName(ctUserFoematRepository.findByUserId(ctReport.getHechaId()).getName());
        }

        String taskId = ctReport.getTaskId();
        //获取时间

        map.put("taskId", taskId);

        //获取时间
        CtTask ctTask = ctTaskService.findByTaskId(id);

        map.put("taskTime", ctTask);

        map.put("reportFormat", reportFormat);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "详情");
        return "/colligate/checks_detail";
    }


    /**
     * @deprecation 案卷回撤
     * @author 雷楚桥
     * @date 18-7-19 下午12:22
     */
    @RequestMapping("/back")
    public String back(Map<String, Object> map) {
        List<CtReportFormatSql> sqls = ctReportFormatSqlRepository.findDiary(5, 7);
        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));

        map.put("formatList", sqls);
        map.put("titleName", "案卷回撤");
        return "/colligate/back";
    }

    /**
     * @param id Integer 案件id
     * @deprecation 案卷回撤详情
     * @author 雷楚桥
     * @date 2018-08-22 15:45
     */
    @RequestMapping("/back_detail")
    public String back_detail(@Param("id") Integer id,
                              Map<String, Object> map) {

        //详情
        CtReport ctReport = reportS.findReportById(id);
        CtReportFormat reportFormat = formatH.ctReportFormat(ctReport);
        reportFormat.setCheckManName(ctUserFoematRepository.findByUserId(ctReport.getCheckManId()).getName());
        if (ctReport.getHechaId() != null) {
            reportFormat.setHechaName(ctUserFoematRepository.findByUserId(ctReport.getHechaId()).getName());
        }
        if (ctReport.getFinishManId() != null) {
            reportFormat.setFinishName(ctUserFoematRepository.findByUserId(ctReport.getFinishManId()).getName());
        }

        String taskId = ctReport.getTaskId();
        //获取时间

        map.put("taskId", taskId);

        //获取时间
        CtTask ctTask = ctTaskService.findByTaskId(id);

        map.put("taskTime", ctTask);

        map.put("reportFormat", reportFormat);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "详情");
        return "/colligate/back_detail";
    }

    /**
     * @deprecation 历史记录
     * @author 雷楚桥
     * @date 18-7-19 下午12:25
     */
    @RequestMapping("/history")
    public String history(Map<String, Object> map,@Param("page") Integer page,HttpSession session) {
//        Integer branchId = authH.thisBranchId();
//        if(branchId != null){
//            List<CtReport> reportList = reportS.findAllReportByBranchId(branchId);
//            System.out.println(authH.thisBranchId());
//            List<CtReportFormat> formatList = formatH.ctReportFormat(reportList);
//            formatList.stream().forEach(format -> System.out.println(format.getCtReport().getId()));
//            map.put("format",formatList);
//        }
        //每页显示的条数
        long pageSize = 20;

        long total = ctReportFormatSqlRepository.countReport(-1, 7);
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        List<CtReportFormatSql> sqls = ctReportFormatSqlRepository.findDiaryByPage(-1, 7, startRow, pageSize);
        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));

        map.put("currentPage", page);

        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));
        map.put("reportList", sqls);
        map.put("titleName", "历史记录");
        return "/colligate/history";
    }

    /**
     * @author 王宇
     * @date 2018/9/11
     * @description 民情日记
     */
    @RequestMapping("/diary")
    public String diary(Map<String, Object> map, @Param("page") Integer page, HttpSession session) {


        //每页显示的条数
        long pageSize = 15;

        long total = ctCopnoteFormatSqlRepository.countAll();
        //查到记录总数
        map.put("reportNum", total);

        System.out.println(total);
        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;

        //民情日记登记
        List<CtCopnoteFormatSql> ctCopnoteFormatSqls = ctCopnoteFormatSqlRepository.findByFromat(startRow, pageSize);
        map.put("copnoteList", ctCopnoteFormatSqls);

        map.put("currentPage", page);


        map.put("titleName", "社情民意上报");
        return "/colligate/diary";
    }

    /**
     * @author  王宇
     * @date 2018/9/18
     * @description  已结案
     */
    @RequestMapping("/haveFinished")
    public String haveFinished(Map<String, Object> map,@Param("page") Integer page,HttpSession session) {

        //每页显示的条数
        long pageSize = 20;

        long total = ctReportFormatSqlRepository.countReport(5, 7);
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;
        List<CtReportFormatSql> sqls = ctReportFormatSqlRepository.findDiaryByPage(5, 7, startRow, pageSize);
        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));

        map.put("currentPage", page);

        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));
        map.put("reportList", sqls);
        map.put("titleName", "已结案");
        return "/colligate/haveFinished";
    }


    /**
     * @author  王宇
     * @date 2018/9/18
     * @description  回撤案件分类
     */
    @RequestMapping("/caseback")
    public String caseback(Map<String, Object> map,@Param("page") Integer page,HttpSession session){

        //每页显示的条数
        long pageSize = 20;

        long total = ctReportFormatSqlRepository.countCaseBack();
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;

        List<CtReportFormatSql> sqls = ctReportFormatSqlRepository.findCaseBack(startRow, pageSize);
        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setScheduleName(Status.REPORT_SCHEDULE.get(ctReportFormatSql.getSchedule())));

        map.put("currentPage", page);

        sqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));
        map.put("reportList", sqls);
        map.put("currentPage", page);

        map.put("titleName", "回撤案件");



        return "/colligate/caseback";
    }

}
